package com.mycode.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.MessageToMessageCodec;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.string.StringDecoder;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;
import java.util.List;

import static com.mycode.util.ByteBufferUtil.debugAll;

@Slf4j
public class HelloServer {

    public static void main(String[] args) {
        // 1. ServerBootstrap 服务启动器，负责组装 netty 组件，启动服务器
        new ServerBootstrap()
                // 2. 线程组
                .group(new NioEventLoopGroup())
                // 3. 选择服务器的 ServerSocketChannel 实现
                .channel(NioServerSocketChannel.class)
                // 4. boss 负责处理连接 worker(child) 负责处理读写，决定了 woker(child) 能执行哪些操作（handler）
                .childHandler(
                        // 5. Channel 代表和客户端进行数据读写的通道，Initializer 初始化，负责添加别的 Handler
                    new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel ch) throws Exception {
                            // 6. 添加具体 的 handler

                            ch.pipeline().addLast(new MessageToMessageEncoder<ByteBuf>() {

                                @Override
                                protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
                                    log.debug("enter DIY Decoder");
                                    ByteBuffer buffer = byteBuf.nioBuffer();
                                    debugAll(buffer);
                                    list.add(buffer);
                                    channelHandlerContext.fireChannelRead(list);
                                }
                            });
                            ch.pipeline().addLast(new StringDecoder()); // 将 ByteBuf 转换为字符串
                            ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { // 自定义 handler
                                // 读事件
                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                    // 打印上一步转换好的字符串
                                    log.debug("1111 : {}", msg);
                                }
                            });
                        }
                })
                // 7. 监听端口
                .bind(8080);
    }

}
